// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-11-11

#ifndef __SVR_ASYNCEXEC_H_COMMON_SVR_BASE__
#define __SVR_ASYNCEXEC_H_COMMON_SVR_BASE__

#pragma once

namespace JoyServer
{

class CJoyServer;
class CJoyAsyncObject;
class CJoyAsyncExec;

class CJoyAsyncWorker
{
public:
    typedef CJoyAsyncObject* RequestType;
    CJoyAsyncExec*  m_executor;

    virtual BOOL Initialize(void* p)
    {
        m_executor = (CJoyAsyncExec*)p;
        return TRUE;
    }
    virtual void Terminate(void* /*pvParam*/)
    {
	}

	void Execute(RequestType pAsyObj, void *, OVERLAPPED* ) throw();
};

class CJoyAsyncExec : public CThreadPool<CJoyAsyncWorker>
{
protected:
    friend class CJoyAsyncObject;
    friend class CJoyAsyncWorker;

    CJoyAtomCount   m_queuedCount;
    int m_threadCount;
    int m_threadIncrement;
    CJoyCriticalSection m_resizeLock;
    CJoyServer* m_server;

    void ResizePool(int queuedCount);
public:
    CJoyAsyncExec();
    virtual ~CJoyAsyncExec();

    virtual bool Create(CJoyServer* pServer, int nThreadCount, int nIncrement = 16);
    virtual void Destroy();

    bool QueueObject(CJoyAsyncObject* asyObj);

    UINT QueuedCount() const
    {
        return m_queuedCount;
    }

};


} // namespace JoyServer

#endif  // __SVR_ASYNCEXEC_H_COMMON_SVR_BASE__